package br.com.gbvbahia.mybooks;

import java.io.Serializable;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.commons.utils.AndroidUtils;
import br.com.gbvbahia.mybooks.dao.PerfilDAO;
import br.com.gbvbahia.mybooks.fragment.FragmentTransfer;
import br.com.gbvbahia.mybooks.fragment.LivroInfoFragment;
import br.com.gbvbahia.mybooks.modelos.LivroVO;
import br.com.gbvbahia.mybooks.service.ws.WebService;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

/*
 * se backgroud para de se repetir
 * public static void fixBackgroundRepeat(View view) {
 * Drawable bg = view.getBackground();
 * if(bg != null) {
 * if(bg instanceof BitmapDrawable) {
 * BitmapDrawable bmp = (BitmapDrawable) bg;
 * bmp.mutate(); // make sure that we aren't sharing state anymore
 * bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
 * }
 * }
 * }
 */
public class Principal extends ActivityMae implements FragmentTransfer {

	private static final int MENU_LOGIN_ID = 11;
	private ImageButton helpButton;
	private WebService service;
	PerfilDAO perfilDAO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.principal);
		service = new WebService(this);
		perfilDAO = new PerfilDAO(this);
		configureActionBar();
		helpButton = (ImageButton) findViewById(br.com.gbvbahia.commons.R.id.actionbar_home_btn);
		helpButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final Dialog dialog = new Dialog(Principal.this);
				dialog.setContentView(R.layout.dialog_help);
				dialog.setTitle(getString(R.string.ajuda));

				TextView text = (TextView) dialog.findViewById(R.id.text);

				Button dialogButton = (Button) dialog
						.findViewById(R.id.dialogButtonOK);
				dialogButton.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				dialog.show();
			}
		});
	}

	private void configureActionBar() {

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle(R.string.app_name);
		actionBar.setHomeAction(new MainAction());
		actionBar.addAction(new EnterAction());
		actionBar.addAction(new CloseAction());
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void updateFragment(int idFragmentToUpdate, Serializable obj) {
		switch (idFragmentToUpdate) {
		case R.layout.listar:
			LivroInfoFragment frag = (LivroInfoFragment) getSupportFragmentManager()
					.findFragmentByTag("livroInfo");
			frag.popularTextsViews((LivroVO) obj);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem menuItem = menu.add(0, MENU_LOGIN_ID, 0,
				getString(R.string.configurarLogin));
		menuItem.setIcon(R.drawable.key);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == MENU_LOGIN_ID) {
			startActivity(new Intent(getBaseContext(), Config.class));
		}
		return true;
	}

	public class MainAction extends AbstractAction {
		public MainAction() {
			super(R.drawable.bookmy_icon_top_2);
		}

		@Override
		public void performAction(View view) {
		}
	}

	private class CloseAction extends AbstractAction {
		public CloseAction() {
			super(R.drawable.logout);
		}

		@Override
		public void performAction(View view) {
			finish();
		}

	}

	private class EnterAction extends AbstractAction {
		public EnterAction() {
			super(R.drawable.sync);
		}

		@Override
		public void performAction(View view) {
			if (!AndroidUtils.isNetworkAvailable(Principal.this)) {
				AndroidUtils.alertDialog(Principal.this, R.string.aviso_13, R.string.app_name);
				return;
			}else if(perfilDAO.getPerfil() == null){
				AndroidUtils.alertDialog(Principal.this, R.string.aviso_53, R.string.app_name);
				return;
			}
			final Dialog dialog = new Dialog(Principal.this);
			dialog.setContentView(R.layout.dialog_sync);
			dialog.setTitle(getString(R.string.sincronizar));
			dialog.show();
			Principal.this.startTransacaoComRede(new SyncActivity(dialog));
		}
	
	}
	
	private class SyncActivity implements Transacao{

		private ImageView img1;
		private ImageView img2;
		private ImageView img3;
		private ImageView img4;
		private Button close;
		private int process;
		private int result1;
		private int result2;
		private int result3;
		private int result4;
		
		private SyncActivity(final Dialog dialog) {
			super();
			img1 = (ImageView)dialog.findViewById(R.id.imgsync_1);
			img2 = (ImageView)dialog.findViewById(R.id.imgsync_2);
			img3 = (ImageView)dialog.findViewById(R.id.imgsync_3);
			img4 = (ImageView)dialog.findViewById(R.id.imgsync_4);
			close = (Button) dialog.findViewById(R.id.btnFechar);
			close.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			process = 0;
		}

		@Override
		public void executar() throws Exception {
			switch (process) {
			case 0:
				result1 = service.enviarLivrosRemovidos();
				break;
			case 1:
				result2 = service.buscarLivrosRemovidos();
				break;
			case 2:
				result3 = service.buscarLivrosWS();
				break;
			case 3:
				result4 = service.enviarLivrosWS();
				break;
			}
			
		}

		@Override
		public void atualizarView() {
			switch (process) {
			case 0:
				img1.setImageResource(R.drawable.send);
				if(result1 == WebService.SINC_EFETUADA_OK){
					img1.setImageResource(R.drawable.ok);
				}else {
					img1.setImageResource(R.drawable.erro);
				}
				process = 1;
				startTransacaoComRede(this);
				break;
			case 1:
				img2.setImageResource(R.drawable.send);
				if(result2 == WebService.SINC_EFETUADA_OK){
					img2.setImageResource(R.drawable.ok);
				}else {
					img2.setImageResource(R.drawable.erro);
				}
				process = 2;
				startTransacaoComRede(this);
				break;
			case 2:
				img3.setImageResource(R.drawable.send);
				if(result3 == WebService.SINC_EFETUADA_OK){
					img3.setImageResource(R.drawable.ok);
				}else {
					img3.setImageResource(R.drawable.erro);
				}
				process = 3;
				startTransacaoComRede(this);
				break;
			case 3:
				img4.setImageResource(R.drawable.send);
				if(result4 == WebService.SINC_EFETUADA_OK){
					img4.setImageResource(R.drawable.ok);
				}else {
					img4.setImageResource(R.drawable.erro);
				}
				process = 0;
				close.setEnabled(true);
				break;
			}
		}
		
	}

}
